/*
package com.xm.xmds1705083.service.netty.client;

import com.xm.xm.util.DateUtils;
import com.xm.xmds1705083.service.model.SocketEntity;
import com.xm.xmds1705083.service.util.CodeUtil;
import com.xm.xmds1705083.service.util.HexCodeUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import net.sf.json.JSONObject;

import java.util.Date;

public class NettyClientHandler extends SimpleChannelInboundHandler<SocketEntity> {

    //利用写空闲发送心跳检测消息
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            switch (e.state()) {
                case WRITER_IDLE:
                    SocketEntity entity = new SocketEntity();
                    entity.setVersion( HexCodeUtils.tenToHexPadLeft( 1,2)+HexCodeUtils.tenToHexPadLeft( 0,2) );
                    entity.setCmd(HexCodeUtils.tenToHexPadLeft( 1,4) );
                    entity.setType(HexCodeUtils.tenToHexPadLeft( 1,4) );
                    entity.setTermid(HexCodeUtils.tenToHexPadLeft( 1,24) );
                    entity.setFlag(HexCodeUtils.tenToHexPadLeft( 0,8) );
                    entity.setSeq(HexCodeUtils.tenToHexPadLeft( 0,8) );
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("datasrc","百胜");
                    jsonObject.put("user","tcUser");
                    jsonObject.put("passwd","tjlgGuCl");
                    entity.setParam( jsonObject.toString());
                    int lengths = (2+2+2+12+4 ) * 2  + jsonObject.toString().getBytes().length ;
                    System.out.println("lengths:"+lengths);
                    entity.setLength(HexCodeUtils.tenToHexPadLeft( lengths,4) );
                    // CRC16校验
                    byte[] crc16_data = CodeUtil.hexStringToBytes(entity.getStart()
                            + entity.getLength() + entity.getVersion()
                            + entity.getCmd() + entity.getType() + entity.getTermid()
                            + entity.getSeq() +entity.getFlag() + entity.getParam() +entity.getEnd() );
                    String crc16 = CodeUtil.CRC16Table(crc16_data);
                    crc16 = crc16.substring(0, 4);
                    entity.setCrc16(crc16);
                    // 1.组装数据 按照规定顺序组装数据
                    byte[] bytes = entity.EnCode();
                    ctx.writeAndFlush(bytes);
                    System.out.println(" 客户端发送请求给服务器端 --------" + DateUtils.getNowDateTime());
                    break;
                default:
                    break;
            }
        }
    }
    @Override
    protected void messageReceived(ChannelHandlerContext channelHandlerContext, SocketEntity baseMsg) throws Exception {
        System.out.println(" reciver from server"+baseMsg.getParam());
        switch (Integer.valueOf(baseMsg.getCmd() ).intValue()  ){
            case 1:{
                baseMsg = new SocketEntity();
                channelHandlerContext.writeAndFlush(baseMsg);
            }break;
            case 2:{
                System.out.println("receive ping from server----------"+baseMsg.toString());
            }break;
            case 3:{
            }break;
            case 4:{
            }break;
            case 5:{
            }break;

            case 41 :
            {
                //--客户端接受到数据 并返回数据给 服务端
                baseMsg.setVersion( HexCodeUtils.tenToHexPadLeft( 1,2)+HexCodeUtils.tenToHexPadLeft( 0,2) );
                baseMsg.setCmd(HexCodeUtils.tenToHexPadLeft( 1,4) );
                baseMsg.setType(HexCodeUtils.tenToHexPadLeft( 1,4) );
                baseMsg.setTermid(HexCodeUtils.tenToHexPadLeft( 1,24) );
                baseMsg.setFlag(HexCodeUtils.tenToHexPadLeft( 0,8) );
                baseMsg.setSeq(HexCodeUtils.tenToHexPadLeft( 0,8) );
                int lengths = (2+2+2+12+4 ) * 2  + baseMsg.getParam().getBytes().length ;
                baseMsg.setLength(HexCodeUtils.tenToHexPadLeft( lengths,4) );
                // CRC16校验
                byte[] crc16_data = CodeUtil.hexStringToBytes(baseMsg.getStart()
                        + baseMsg.getLength() + baseMsg.getVersion()
                        + baseMsg.getCmd() + baseMsg.getType() + baseMsg.getTermid()
                        + baseMsg.getSeq() +baseMsg.getFlag() + baseMsg.getParam() +baseMsg.getEnd() );
                String crc16 = CodeUtil.CRC16Table(crc16_data);
                crc16 = crc16.substring(0, 4);
                baseMsg.setCrc16(crc16);
                byte[] bytes = baseMsg.EnCode();
                channelHandlerContext.writeAndFlush(bytes);
            }break;
            default:break;
        }
    }

}
*/
